﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

/// <summary>
/// Summary description for clsUserLoginDAL
/// </summary>
namespace InstantNaukriDAL
{
    public class clsUserLoginDAL
    {
        InstantNaukriDataContextDataContext objInstantNaukriDataContextDataContext = new InstantNaukriDataContextDataContext();
        public clsUserLoginDAL()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public List<IN_TB_USER> GetAllUsers()
        {
            try
            {
                List<IN_TB_USER> objIN_TB_USER = new List<IN_TB_USER>();
                objIN_TB_USER = (from obj in objInstantNaukriDataContextDataContext.IN_TB_USERs
                                 select obj).ToList();
                return objIN_TB_USER;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IN_TB_USER GetUserByUserNamePassword(string UserName, string Password)
        {
            try
            {
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                objIN_TB_USER = (from obj in objInstantNaukriDataContextDataContext.IN_TB_USERs
                                 where obj.LOGIN_ID.ToLower() == UserName.ToLower() && obj.PASSWORD.ToLower() == Password.ToLower()
                                 select obj).SingleOrDefault();
                return objIN_TB_USER;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IN_TB_USER GetUserByID(int ID)
        {
            try
            {
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                objIN_TB_USER = (from obj in objInstantNaukriDataContextDataContext.IN_TB_USERs
                                 where obj.ID == ID
                                 select obj).SingleOrDefault();
                return objIN_TB_USER;
            }
            catch (Exception)
            {

                throw;
            }
        }


        public IN_TB_USER GetUserBySubBranchID(int ID)
        {
            try
            {

                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                objIN_TB_USER = (from obj in objInstantNaukriDataContextDataContext.IN_TB_USERs
                                 join objSubBranch in objInstantNaukriDataContextDataContext.IN_TB_SUBBRANCH_MSTs on obj.ID equals objSubBranch.USER_ID
                                 where objSubBranch.ID == ID
                                 select obj).SingleOrDefault();
                return objIN_TB_USER;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void DeleteUser(int UserID)
        {
            try
            {
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                objIN_TB_USER = (from obj in objInstantNaukriDataContextDataContext.IN_TB_USERs
                                 where obj.ID == UserID
                                 select obj).SingleOrDefault();
                if (objIN_TB_USER != null)
                {
                    objInstantNaukriDataContextDataContext.IN_TB_USERs.DeleteOnSubmit(objIN_TB_USER);
                    objInstantNaukriDataContextDataContext.SubmitChanges();
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<IN_TB_USER> GetSuperAdminByRoleID(int ID)
        {
            try
            {
                List<IN_TB_USER> objIN_TB_USER = new List<IN_TB_USER>();
                objIN_TB_USER = (from obj in objInstantNaukriDataContextDataContext.IN_TB_USERs
                                 where obj.ROLEID == ID
                                 select obj).ToList();
                return objIN_TB_USER;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IN_TB_USER GetUserByBranchID(int ID)
        {
            try
            {

                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                objIN_TB_USER = (from obj in objInstantNaukriDataContextDataContext.IN_TB_USERs
                                 join objBranch in objInstantNaukriDataContextDataContext.IN_TB_BRANCH_MSTs on obj.ID equals objBranch.USER_ID
                                 where objBranch.ID == ID
                                 select obj).SingleOrDefault();
                return objIN_TB_USER;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IN_TB_USER GetUserByEmployerID(int ID)
        {
            try
            {

                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                objIN_TB_USER = (from obj in objInstantNaukriDataContextDataContext.IN_TB_USERs
                                 join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on obj.ID equals objEmployer.USER_ID
                                 where objEmployer.ID == ID
                                 select obj).SingleOrDefault();
                return objIN_TB_USER;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public int SaveUser(IN_TB_USER objIN_TB_USER)
        {
            try
            {
                IN_TB_USER objIN_TB_USEROLD = new IN_TB_USER();
                objIN_TB_USEROLD = (from obj in objInstantNaukriDataContextDataContext.IN_TB_USERs
                                    where obj.ID == objIN_TB_USER.ID
                                    select obj).SingleOrDefault();
                if (objIN_TB_USEROLD != null)
                {
                    objIN_TB_USEROLD.ID = objIN_TB_USER.ID;
                    objIN_TB_USEROLD.LOGIN_ID = objIN_TB_USER.LOGIN_ID;
                    objIN_TB_USEROLD.PASSWORD = objIN_TB_USER.PASSWORD;
                    objIN_TB_USEROLD.ROLEID = objIN_TB_USER.ROLEID;
                    objIN_TB_USEROLD.EMAIL = objIN_TB_USER.EMAIL;
                    objIN_TB_USEROLD.MOBILE = objIN_TB_USER.MOBILE;
                    objIN_TB_USEROLD.NAME = objIN_TB_USER.NAME;
                    objIN_TB_USEROLD.DOB = objIN_TB_USER.DOB;
                    objIN_TB_USEROLD.STATUS = objIN_TB_USER.STATUS;
                    objIN_TB_USEROLD.CREATED_BY = objIN_TB_USER.CREATED_BY;
                    objIN_TB_USEROLD.CREATED_ON = objIN_TB_USER.CREATED_ON;
                    objIN_TB_USEROLD.MODIFIED_BY = objIN_TB_USER.MODIFIED_BY;
                    objIN_TB_USEROLD.MODIFIED_ON = objIN_TB_USER.MODIFIED_ON;
                    objInstantNaukriDataContextDataContext.SubmitChanges();
                    return objIN_TB_USEROLD.ID;
                }
                else
                {
                    objInstantNaukriDataContextDataContext.IN_TB_USERs.InsertOnSubmit(objIN_TB_USER);
                    objInstantNaukriDataContextDataContext.SubmitChanges();
                    return objIN_TB_USER.ID;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
